package com.ming.mapper;

import com.ming.pojo.entity.Emp;
import com.ming.pojo.vo.EmpListVO;
import com.ming.pojo.dto.EmpQueryDTO;
import com.ming.pojo.vo.EmpVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EmpMapper {

    @Select("SELECT * FROM emp ORDER BY create_time DESC")
    List<Emp> list();

    List<Emp> listByCondition(EmpQueryDTO empQueryDTO);

    List<EmpListVO> listVOByCondition(EmpQueryDTO empQueryDTO);

    @Select("SELECT * FROM emp WHERE id = #{id}")
    Emp getById(Integer id);

    @Select("SELECT * FROM emp WHERE id = #{id}")
    Emp findById(Integer id);

    @Select("SELECT e.*, d.name as deptName FROM emp e LEFT JOIN dept d ON e.dept_id = d.id WHERE e.id = #{id}")
    com.ming.pojo.vo.EmpVO getEmpVOById(Integer id);

    @Insert("INSERT INTO emp(username, password, name, gender, phone, job, salary, image, entry_date, dept_id, create_time, update_time) " +
            "VALUES (#{username}, #{password}, #{name}, #{gender}, #{phone}, #{job}, #{salary}, #{image}, #{entryDate}, #{deptId}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Emp emp);

    @Update("UPDATE emp SET name = #{name}, gender = #{gender}, phone = #{phone}, job = #{job}, salary = #{salary}, image = #{image}, " +
            "entry_date = #{entryDate}, dept_id = #{deptId}, update_time = #{updateTime} WHERE id = #{id}")
    void update(Emp emp);

    @Delete("DELETE FROM emp WHERE id = #{id}")
    void deleteById(Integer id);

    @Select("SELECT COUNT(*) FROM emp WHERE name = #{name} OR phone = #{phone} OR username = #{username}")
    int countByNameOrPhoneOrUsername(@Param("name") String name, @Param("phone") String phone, @Param("username") String username);

    @Select("SELECT COUNT(*) FROM emp WHERE (name = #{name} OR phone = #{phone} OR username = #{username}) AND id != #{id}")
    int countByNameOrPhoneOrUsernameExcludeId(@Param("name") String name, @Param("phone") String phone, @Param("username") String username, @Param("id") Integer id);

    int countByCondition(EmpQueryDTO empQueryDTO);

    @Select("SELECT * FROM emp WHERE username = #{username}")
    Emp getByUsername(String username);

    @Select("SELECT COUNT(*) FROM emp WHERE dept_id = #{deptId}")
    int countByDeptId(Integer deptId);
}